<template>
  <div class="count-box">
    <button class="minus" @click="decrement">-</button>
    <input :value="value" @change="handleInput" type="text" class="count-input">
    <button class="plus" @click="increment">+</button>
  </div>
</template>

<script>

export default {
  props: {
    value: {
      type: Number,
      default: 1
    }
  },
  methods: {
    increment () {
      this.$emit('input', this.value + 1)
    },
    decrement () {
      if (this.value > 1) { this.$emit('input', this.value - 1) }
    },
    handleInput (e) {
      const value = parseInt(e.target.value)
      if (isNaN(value) || value < 1) {
        e.target.value = this.value
      } else {
        this.$emit('input', value)
      }
    }
  }
}
</script>

<style scoped lang="less">
  .count-box {
    width: 110px;
    display: flex;
    .minus,.plus {
      width: 30px;
      height: 30px;
      line-height: 30px;
      text-align: center;
      background-color: #f5f5f5;
      border: none;
      cursor: pointer;
    }
    .minus {
      border-radius: 4px 0 0 4px ;
    }
    .plus {
      border-radius: 0 4px 4px 0 ;
    }
    .count-input{
      width: 40px;
      height: 30px;
      line-height: 30px;
      text-align: center;
      border: none;
      margin: 0 5px;
      background-color: #f5f5f5;
    }
  }

</style>
